﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSysCore;
using WebSysCore.Helpers;

namespace HSchSchedule.FrontPage
{
    public partial class UpdateCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            int cusID = int.Parse(Request["CustomerID"]);
            Customer enCus = DataBase.GetEnCustomer(cusID);

            txtCustomerID.Text = enCus.CustomerId.ToString();
            txtCustomerName.Text = enCus.CustomerName;
            txtCustomerPhone.Text = enCus.CustomerPhone;
            txtDistrict.Text = enCus.CustomerDistrictOrCity;
            txtEmail.Text = enCus.Email;
            txtAddress.Text = enCus.CustomerAddress;
            txtBankAcount.Text = enCus.BankAccount;
            txtBankName.Text = enCus.BankName;
            txtContactName.Text = enCus.ContactName;
            txtContactPhone.Text = enCus.ContactPhone;

            List<Province> lst = DataBase.GetAllProvince();
            dropProvince.DataSource = lst;
            dropProvince.DataBind();
            dropProvince.SelectedValue = enCus.CustomerProvinceId.ToString();

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            // throw new NotImplementedException();
            int cusID = int.Parse(Request["CustomerID"]);
            Customer enCus = DataBase.GetEnCustomer(cusID);
            Customer newCus = new Customer();
            newCus.BankAccount = txtBankAcount.Text.Trim();
            newCus.BankName = txtBankName.Text.Trim();
            newCus.ContactName = txtContactName.Text.Trim();
            newCus.ContactPhone = txtContactPhone.Text.Trim();
            newCus.CustomerAddress = txtAddress.Text.Trim();
            newCus.CustomerDistrictOrCity = txtDistrict.Text.Trim();
            newCus.CustomerName = txtCustomerName.Text.Trim();
            newCus.CustomerPhone = txtCustomerPhone.Text.Trim();
            newCus.CustomerProvinceId = int.Parse(dropProvince.SelectedValue);
            newCus.Email = txtEmail.Text.Trim();
            newCus.LoginName = enCus.LoginName; ;
            newCus.Password = enCus.Password;
            newCus.Deleted = enCus.Deleted;

            string err = "";

            if (!ValidateHelp.ValidateCustomer(newCus, out err))
            {
                lblError.Text = err;
            }
            else
            {
                DataBase.UpdateCustomer(enCus, newCus);
                Response.Redirect("~/FrontPage/CustomerManage.aspx");
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/FrontPage/CustomerManage.aspx");
        }
    }
}
